package cn.itcast.bos.web.action.take_delivery;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.bos.web.action.common.BaseAction;

@Namespace("/")
@ParentPackage("struts-default")
@Scope("prototype")
@Controller
public class WayBillAction extends BaseAction<WayBill> {
	private static final long serialVersionUID = 1L;

	private static final Logger LOGGER = Logger.getLogger(WayBillAction.class);
	@Autowired
	private WayBillService wayBillService;

	// 添加运单
	@Action(value = "wayBill_save" )
	public void save() throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			//解决保存运单时,保存一个瞬时态的订单报错
			if (model.getOrder() != null && model.getOrder().getId() == null) {
				model.setOrder(null);
			}
			wayBillService.save(model);
			// 保存成功
			result.put("success", true);
			result.put("msg", "保存运单成功！");
			LOGGER.info("保存运单成功，运单号：" + model.getWayBillNum());
		} catch (Exception e) {
			e.printStackTrace();
			// 保存失败
			result.put("success", false);
			result.put("msg", "保存运单失败！");
			LOGGER.error("保存运单失败，运单号：" + model.getWayBillNum(), e);
		}
		String jsonString = JSONArray.toJSONString(result, SerializerFeature.DisableCircularReferenceDetect);
		ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(jsonString);
	}

	// 分页查询
	@Action(value = "wayBill_pageQuery")
	public void pageQuery() throws IOException {
		Sort sort = new Sort(Direction.DESC, "id");
		Pageable pageable = new PageRequest(page - 1, rows, sort);
		Page<WayBill> pageData = wayBillService.pageQuery(model,pageable);
		pageDataJsonToWeb(pageData);
	}

	// 根据运单号来查询运单
	@Action(value = "wayBill_findByWayBillNum")
	public void findByWayBillNum() throws IOException {
		WayBill wayBill = wayBillService.findByWayBillNum(model.getWayBillNum());
		Map<String, Object> result = new HashMap<String, Object>();
		if (wayBill != null) {
			result.put("success", true);
			result.put("wayBillData", wayBill);
		} else { 
			result.put("success", false);
		}
		String jsonString = JSONArray.toJSONString(result, SerializerFeature.DisableCircularReferenceDetect);
		ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(jsonString);
	}
}
